import dayjs from "dayjs";

function transformLink(url: string) {
  if (url?.includes("https://etonkids-file.oss-cn-beijing.aliyuncs.com")) {
    return `https://assesstest.kafamily.cn/img?l=${url}`;
  }
  return url;
}
export default function (list: any[]) {
  let htmlText = `
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>活动列表</title>
</head>
<style>details{border:1px solid #9254de; padding:4px 8px;margin:10px;}</style>
<body>
<h2>共计${list.length}个活动</h2>
${list
  .map((item, index) => {
    return `<details>
      <summary>
      <span style="margin:0 5px; color:#1E6B9B;">${
        item.weekdayStr
      }</span>|<span style="margin:0 5px; color:#D4AF37;">${dayjs(
      item.timeAt[0]
    ).format("MM-DD HH:mm")}~${dayjs(item.timeAt[1]).format("HH:mm")}</span>|
    <span style="margin:0 5px; color:#333333;">${index + 1}.${item.name}</span>
</summary>

      <ul>

        ${
          item.link
            ? `<li>活动链接 :
         <a target="_blank" href="${transformLink(item.link)}" >${transformLink(
                item.link
              )}</a>
        </li>`
            : ""
        }
        ${
          item.signLink
            ? `<li>报名链接 :
                <a target="_blank" href="${transformLink(
                  item.signLink
                )}" >${transformLink(item.signLink)}</a>
                </li>`
            : ""
        }
        ${
          item.organizerName || item.coOrganizerName
            ? `<li>主办方 : ${item.organizerName || ""} ${
                item.coOrganizerName || ""
              }</li>`
            : ""
        }
        ${item.peopleNumber ? `<li>活动人数 : ${item.peopleNumber}</li>` : ""}

      </ul>
    </details>`;
  })
  .join("")}
</body>
</html>
  `;
  var a3: any = (
    <>
      <h2>共计${list.length}个活动</h2>
      {list.map((item, index) => {
        return (
          <details>
            <summary>
              ${index + 1} ${item.name} $
              {dayjs(item.timeAt[0]).format("MM-DD HH:mm")}~$
              {dayjs(item.timeAt[1]).format("HH:mm")} -- ${item.weekdayStr}
            </summary>
            <p>这里是折叠内容区域。支持任意HTML元素，如列表、图片等。</p>
            <ul>
              <li>列表项1</li>
              <li>列表项2</li>
            </ul>
          </details>
        );
      })}
    </>
  );

  const blob = new Blob([htmlText], { type: "text/html" });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = "活动列表.html";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}
